package service

import (
	"context"
	"encoding/json"
	"fx_swift_api/api"
	"fx_swift_api/internal/model"
	"fx_swift_api/pkg/code"
	"fx_swift_api/pkg/global"
	"fx_swift_api/pkg/gorsa"
	"fx_swift_api/pkg/internalerrors"
	"fx_swift_api/pkg/logger"
	"fx_swift_api/pkg/utils"
	"github.com/gogf/gf/v2/container/gvar"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/golang-module/carbon/v2"
	"github.com/shopspring/decimal"
	"github.com/spf13/cast"
)

type ICardSrv interface {
	// 持卡人申请
	CardHolder(ctx context.Context, req *api.CardHolderReq) (string, error)

	// 持卡人信息
	GetCardHolderInfo(ctx context.Context, req *api.GetCardHolderInfoReq) (*api.GetCardHolderRes, error)

	// 开卡申请
	CardApply(ctx context.Context, req *api.CardApplyVoReq) (*api.CardApplyVoRes, error)

	// 销卡申请
	CloseCard(ctx context.Context, req *api.CloseCardReq) error

	// 获取卡信息
	GetCardInfo(ctx context.Context, req *api.GetCardInfoReq) (*api.CardInfoVoRes, error)

	// 卡片信息修改(状态)
	EditCardStatus(ctx context.Context, req *api.EditCardStatusReq) error

	// 充值
	CardRecharge(ctx context.Context, req *api.CardRechargeReq) error

	// 退款
	CardRefund(ctx context.Context, req *api.CardRefundReq) error

	// 获取卡头信息
	GetCardHead(ctx context.Context) (string, error)

	//查询余额
	GetBalance(ctx context.Context) (string, error)
}

type cardSrv struct {
}

func NewCardSrv() ICardSrv {
	return &cardSrv{}
}

// 查询余额
func (c *cardSrv) GetBalance(ctx context.Context) (string, error) {
	var (
		err        error
		balance    string
		accountObj *model.FxAccount
	)
	balance = "0"
	account := ctx.Value("Account").(string)
	if err = global.GFDB.Model("fx_account").Where("account", account).Scan(&accountObj); err != nil {
		logger.SugarLogger.Error(err)
		return "", internalerrors.New(code.ErrUnknown, "数据查询异常")
	}
	if accountObj != nil {
		balance = accountObj.Balance.String()
	}
	return balance, err
}

// 持卡人申请
func (c *cardSrv) CardHolder(ctx context.Context, req *api.CardHolderReq) (string, error) {
	var (
		err            error
		url            = "/api/vas/cardhodler/apply"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CardHolderRes
	)
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return "", internalerrors.New(code.ErrUnknown, err.Error())
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return "", internalerrors.New(code.ErrUnknown, err.Error())
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return "", internalerrors.New(code.ErrUnknown, err.Error())
	}
	if len(pubdecrypt_str) != 0 {
		if _, err = global.GFDB.Ctx(ctx).Model("fx_cardholder").Insert(model.FxCardholder{
			Account:        ctx.Value("Account").(string),
			CardUserId:     resp.CardUserId,
			FirstName:      req.FirstName,
			LastName:       req.LastName,
			MiddleName:     req.MiddleName,
			Email:          req.Email,
			MobilePreFix:   req.MobilePrefix,
			Mobile:         req.Mobile,
			BirthDate:      req.BirthDate,
			CountryCode:    req.CountryCode,
			BillingState:   req.BillingState,
			BillingCity:    req.BillingCity,
			BillingAddress: req.BillingAddress,
			BillingZipCode: req.BillingZipCode,
			Remark:         req.Remark,
			HolderStatus:   "3",
		}); err != nil {
			logger.SugarLogger.Error(err.Error())
			return "", internalerrors.New(code.ErrUnknown, "申请异常")
		}
	}
	return resp.CardUserId, err
}

// 获取持卡人信息
func (c *cardSrv) GetCardHolderInfo(ctx context.Context, req *api.GetCardHolderInfoReq) (*api.GetCardHolderRes, error) {
	var (
		err            error
		url            = "/api/vas/cardhodler/info"
		dataContent    string
		pubdecrypt_str string
		resp           *api.GetCardHolderRes
	)
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "获取信息失败")
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "获取信息失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "获取信息失败")
	}
	return resp, err
}

// 开卡申请
func (c *cardSrv) CardApply(ctx context.Context, req *api.CardApplyVoReq) (*api.CardApplyVoRes, error) {
	var (
		err            error
		url            = "/api/vas/card/apply"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CardApplyVoRes
		agentObj       *model.FxAgent
		accountStr     string
		accountObj     *model.FxAccount
		finalBalance   float64
		endDate        string
	)

	accountStr = ctx.Value("Account").(string)

	if err = global.GFDB.Model("fx_agent").Where("account", accountStr).Scan(&agentObj); err != nil {
		logger.SugarLogger.Error(err)
		return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
	}

	if err = global.GFDB.Ctx(ctx).Model("fx_account").
		Where("account=?", accountStr).Scan(&accountObj); err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
	}

	if accountObj.Balance.LessThan(decimal.NewFromFloat(0)) {
		return nil, internalerrors.New(code.ErrUnknown, "帐号余额不足，请先充值")
	}

	finalBalance = agentObj.Fee * cast.ToFloat64(req.AuthLimitAmount)

	if GetNumerFirst(req.CardHead) == "4" {
		endDate = carbon.Now().AddDays(730).ToDateString()
	} else if GetNumerFirst(req.CardHead) == "5" {
		endDate = carbon.Now().AddDays(730).ToDateString()
	}

	reqObj := api.CardApplyReq{
		UserReqNo:           utils.GetHashCode(utils.GenerateCode32()),
		LocalCurrency:       req.LocalCurrency,
		StartDate:           carbon.Now().ToDateString(),
		EndDate:             endDate,
		AuthLimitAmount:     req.AuthLimitAmount,
		EnableCurrencyCheck: 0,
		EnableMultiUse:      "1",
		BinRangeId:          req.CardHead,
		ChannelType:         "1",
		CardUserId:          req.CardUserId,
		CardAlias:           req.CardAlias,
	}

	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, reqObj)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
	}
	if len(pubdecrypt_str) != 0 {
		if _, err = global.GFDB.Ctx(ctx).Model("fx_card_info").Insert(&model.FxCardInfo{
			CardId:        resp.CardId,
			CardUserId:    req.CardUserId,
			CardAlias:     req.CardAlias,
			Account:       ctx.Value("Account").(string),
			CardNo:        resp.CardNo,
			LocalCurrency: req.LocalCurrency,
			CardVerifyNo:  resp.CardVerifyNo,
			Expiration:    resp.CardExpiryDate,
			CardStatus:    "3",
			CardArea:      req.CardArea,
			CardHead:      req.CardHead,
			BusinessScene: req.BusinessScene,
		}); err != nil {
			logger.SugarLogger.Error(err.Error())
			return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
		}
		err = requestCardInfo(resp.CardId)
		if err = DeductionBalance(finalBalance, accountStr, "admin", model.Fee, "充值手续费"); err != nil {
			logger.SugarLogger.Error(err.Error())
			return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
		}
		if err = DeductionAgentBalance(cast.ToFloat64(req.AuthLimitAmount), accountStr, model.Transfer, "转账"); err != nil {
			logger.SugarLogger.Error(err.Error())
			return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
		}
		if agentObj.CardOpeningFee > 0 {
			if err = DeductionBalance(agentObj.CardOpeningFee, accountStr, "admin", model.OpenCard, "开卡费"); err != nil {
				logger.SugarLogger.Error(err.Error())
				return nil, internalerrors.New(code.ErrUnknown, "开卡申请失败")
			}
		}
	}
	return resp, err
}

// 销卡申请
func (c *cardSrv) CloseCard(ctx context.Context, req *api.CloseCardReq) error {
	var (
		err            error
		url            = "/api/vas/card/close"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CloseCardRes
	)
	req.UserReqNo = utils.GetHashCode(utils.GenerateCode32())
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "销卡申请失败")
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "销卡申请失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "销卡申请失败")
	}
	if len(pubdecrypt_str) == 0 {
		logger.SugarLogger.Error("销卡申请失败")
		return internalerrors.New(code.ErrUnknown, "销卡申请失败")
	}
	return err
}

// 获取卡信息
func (c *cardSrv) GetCardInfo(ctx context.Context, req *api.GetCardInfoReq) (*api.CardInfoVoRes, error) {
	var (
		err           error
		cardInfo      *model.FxCardInfo
		cardHolderObj model.FxCardholder
		result        = &api.CardInfoVoRes{}
	)
	err = requestCardInfo(req.CardId)

	if err = global.GFDB.Ctx(ctx).Model("fx_card_info").
		Where("card_id=?", req.CardId).Scan(&cardInfo); err != nil {
		logger.SugarLogger.Error(err)
		return nil, internalerrors.New(code.ErrUnknown, "读取数据异常")
	}
	if cardInfo == nil {
		return nil, internalerrors.New(code.ErrUnknown, "卡号异常")
	}
	if len(cardInfo.CardUserId) > 0 {
		// 获取持卡人账单地址
		if err = global.GFDB.Ctx(ctx).Model("fx_cardholder").
			Where("card_user_id=?", cardInfo.CardUserId).Scan(&cardHolderObj); err != nil {
			return nil, internalerrors.New(code.ErrUnknown, err.Error())
		}
	}

	billAddressObj := api.BillAddressInfo{
		Country:        cardHolderObj.Country,
		CountryCode:    cardHolderObj.CountryCode,
		BillingState:   cardHolderObj.BillingState,
		BillingCity:    cardHolderObj.BillingCity,
		BillingAddress: cardHolderObj.BillingAddress,
		BillingZipCode: cardHolderObj.BillingZipCode,
	}
	cardUserInfoObj := api.CardUserInfo{
		FirstName: cardHolderObj.FirstName,
		LastName:  cardHolderObj.LastName,
		Email:     cardHolderObj.Email,
		Mobile:    cardHolderObj.Mobile,
		BirthDate: cardHolderObj.BirthDate,
	}
	result.CardNo = cardInfo.CardNo
	result.CardVerifyNo = cardInfo.CardVerifyNo
	result.CardExpiryDate = cardInfo.Expiration
	result.LocalCurrency = cardInfo.LocalCurrency
	result.StartActiveDate = cardInfo.StartActiveDate
	result.EndCloseDate = cardInfo.EndCloseDate
	result.CreditLimitAmt = cardInfo.CreditLimitAmt
	result.BalanceAmt = cardInfo.BalanceAmt
	result.UsedAuthAmt = cardInfo.UsedAuthAmt
	result.CardClosedAmt = cardInfo.CardClosedAmt
	result.CardStatus = cardInfo.CardStatus
	result.CardStatusDesc = cardInfo.CardStatusDesc
	result.CreateTime = cardInfo.CreateCardTime
	result.CardUserInfo = cardUserInfoObj
	result.BillAddressInfo = billAddressObj
	return result, err
}

// 卡片信息修改(状态)
func (c *cardSrv) EditCardStatus(ctx context.Context, req *api.EditCardStatusReq) error {
	var (
		err            error
		url            = "/api/vas/card/modifyCard"
		dataContent    string
		pubdecrypt_str string
		resp           *api.EditCardStatusRes
		statusDesc     string
	)
	req.UserReqNo = utils.GetHashCode(utils.GenerateCode32())
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "卡信息修改失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "卡信息修改失败")
	}
	if len(pubdecrypt_str) != 0 {
		if req.Status == "1" {
			statusDesc = "使用中"
		} else if req.Status == "0" {
			statusDesc = "冻结"
		}
		if _, err = global.GFDB.Ctx(ctx).Schema("card").Model("fx_card_info").Data(g.Map{
			"card_status":      req.Status,
			"card_status_desc": statusDesc,
		}).Where("card_id=?", req.CardId).Update(); err != nil {
			logger.SugarLogger.Error(err.Error())
			return internalerrors.New(code.ErrUnknown, "卡信息修改失败")
		}
	}
	return err
}

// 充值
func (c *cardSrv) CardRecharge(ctx context.Context, req *api.CardRechargeReq) error {
	var (
		err            error
		url            = "/api/vas/card/recharge"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CardRechargeRes
		userReqNo      string
		accountStr     string
		accountObj     *model.FxAccount
		agentObj       *model.FxAgent
		finalBalance   float64
		arrearsBalance float64
		redisResult    *gvar.Var
	)
	g.Dump(req)
	accountStr = ctx.Value("Account").(string)

	if err = global.GFDB.Model("fx_agent").Where("account", accountStr).Scan(&agentObj); err != nil {
		return internalerrors.New(code.ErrUnknown, "充值失败")
	}

	if err = global.GFDB.Ctx(ctx).Model("fx_account").
		Where("account=?", accountStr).Scan(&accountObj); err != nil {
		return internalerrors.New(code.ErrUnknown, "充值失败")
	}

	logger.SugarLogger.Infof("代理商账户余额：%s", accountObj.Balance.String())

	reqAmount, _ := decimal.NewFromString(req.AuthLimitAmount)
	if accountObj.Balance.LessThanOrEqual(decimal.NewFromFloat(0.0)) ||
		reqAmount.GreaterThan(accountObj.Balance) {
		return internalerrors.New(code.ErrUnknown, "帐号余额不足，请先充值")
	}

	finalBalance = agentObj.Fee * cast.ToFloat64(req.AuthLimitAmount)

	userReqNo = utils.GetHashCode(utils.GenerateCode32())
	req.UserReqNo = userReqNo
	req.ChannelType = "1"
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "充值失败")
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "充值失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "充值失败")
	}
	if len(pubdecrypt_str) != 0 {
		// 判断改cardId是否有过欠费
		redisResult, err = g.Redis().Get(ctx, req.CardId+"-arrearsBalance")
		if err != nil {
			logger.SugarLogger.Error(err.Error())
			return internalerrors.New(code.ErrUnknown, "帐号余额不足，请先充值")
		}
		if redisResult.Val() != nil {
			arrearsBalance = redisResult.Float64()
			logger.SugarLogger.Infof("卡所欠金额：%f", arrearsBalance)
			err = NewCardSrv().CardRefund(ctx, &api.CardRefundReq{
				CardId:       req.CardId,
				RefundAmount: cast.ToString(arrearsBalance),
				FlagBool:     true,
			})
			if err != nil {
				logger.SugarLogger.Error(err.Error())
				return internalerrors.New(code.ErrUnknown, "帐号余额不足，请先充值")
			}
			// 清除Redis中的key
			_, err = g.Redis().Del(ctx, req.CardId+"-arrearsBalance")
			if err != nil {
				logger.SugarLogger.Error(err.Error())
				return internalerrors.New(code.ErrUnknown, "帐号余额不足，请先充值")
			}
		}
		if err = DeductionBalance(finalBalance, accountStr, "admin", model.Fee, "充值手续费"); err != nil {
			logger.SugarLogger.Error(err.Error())
			return internalerrors.New(code.ErrUnknown, "充值失败")
		}
		if err = DeductionAgentBalance(cast.ToFloat64(req.AuthLimitAmount), accountStr, model.Transfer, "转账费"); err != nil {
			logger.SugarLogger.Error(err.Error())
			return internalerrors.New(code.ErrUnknown, "充值失败")
		}
	}
	return err
}

// 退款
func (c *cardSrv) CardRefund(ctx context.Context, req *api.CardRefundReq) error {
	var (
		err            error
		url            = "/api/vas/card/refund"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CardRefundRes
		userReqNo      string
		account        string
	)
	account = ctx.Value("Account").(string)
	userReqNo = utils.GetHashCode(utils.GenerateCode32())
	req.UserReqNo = userReqNo
	req.ChannelType = "1"
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, req)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "退款失败")
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		logger.SugarLogger.Error(err.Error())
		return internalerrors.New(code.ErrUnknown, "退款失败")
	}
	if len(pubdecrypt_str) != 0 {
		if !req.FlagBool {
			if err = RefundBalance(cast.ToFloat64(req.RefundAmount), account, "admin", model.Refund, "退款"); err != nil {
				logger.SugarLogger.Error(err.Error())
				return internalerrors.New(code.ErrUnknown, "退款失败")
			}
		}
	}
	return err
}

// 获取卡头信息
func (c *cardSrv) GetCardHead(ctx context.Context) (string, error) {
	var (
		err     error
		headObj *model.FxCardHead
	)
	if err = global.GFDB.Ctx(ctx).Model("fx_card_head").Fields("json").Scan(&headObj); err != nil {
		return "", internalerrors.New(code.ErrUnknown, err.Error())
	}
	return headObj.Json, err
}

func requestCardInfo(cardId string) error {
	var (
		err            error
		url            = "/api/vas/card/info"
		dataContent    string
		pubdecrypt_str string
		resp           *api.CardInfoRes
		cardInfoObj    = &model.FxCardInfo{}
	)
	dataContent, err = OperatorParams(global.ViperObj.GetString("gep.url")+url, &api.GetCardInfoReq{
		CardId: cardId,
	})
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	// 公钥解密
	pubdecrypt_str, err = gorsa.PublicDecrypt(dataContent, gorsa.GetPublicKeyStr())
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	err = json.Unmarshal([]byte(pubdecrypt_str), &resp)
	if err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	if err = global.GFDB.Schema("card").Model("fx_card_info").
		Where("card_id=?", cardId).
		Scan(&cardInfoObj); err != nil {
		return internalerrors.New(code.ErrUnknown, err.Error())
	}
	if len(pubdecrypt_str) > 0 {
		if _, err = global.GFDB.Schema("card").Model("fx_card_info").Data(g.Map{
			"start_active_date":     resp.StartActiveDate,
			"end_close_date":        resp.EndCloseDate,
			"credit_limit_amt":      resp.CreditLimitAmt,
			"min_auth_amt":          resp.MinAuthAmt,
			"max_auth_amt":          resp.MaxAuthAmt,
			"balance_amt":           resp.BalanceAmt,
			"used_auth_amt":         resp.UsedAuthAmt,
			"enable_multi_use":      resp.EnableMultiUse,
			"enable_currency_check": resp.EnableCurrencyCheck,
			"card_closed_amt":       resp.CardClosedAmt,
			"card_status":           resp.CardStatus,
			"card_status_desc":      resp.CardStatusDesc,
			"create_card_time":      resp.CreateTime,
		}).Where("card_id=?", cardId).Update(); err != nil {
			return internalerrors.New(code.ErrUnknown, err.Error())
		}

		if _, err = global.GFDB.Schema("card").Model("fx_cardholder").Data(g.Map{
			"country":          resp.BillAddressInfo.Country,
			"country_code":     resp.BillAddressInfo.CountryCode,
			"billing_state":    resp.BillAddressInfo.BillingState,
			"billing_city":     resp.BillAddressInfo.BillingCity,
			"billing_address":  resp.BillAddressInfo.BillingAddress,
			"billing_zip_code": resp.BillAddressInfo.BillingZipCode,
			"first_name":       resp.CardUserInfo.FirstName,
			"last_name":        resp.CardUserInfo.LastName,
			//"mobile":           resp.CardUserInfo.Mobile,
			"email":      resp.CardUserInfo.Email,
			"birth_date": resp.CardUserInfo.BirthDate,
		}).Where("card_user_id=?", cardInfoObj.CardUserId).Update(); err != nil {
			return internalerrors.New(code.ErrUnknown, err.Error())
		}
	}
	return err
}

func GetNumerFirst(cardHead string) string {
	firstDigitChar := cardHead[0] // 获取字符串的第一个字符
	// 如果你想得到这个字符对应的数字
	firstDigit := firstDigitChar - '0' // 将字符转换为对应的整数
	return cast.ToString(firstDigit)
}
